﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
//using UFC.DBUtility;
using UFC.Common;
using Newtonsoft.Json;
using System.Configuration;
using Newtonsoft.Json.Linq;
using System.Xml;
using System.Collections;

namespace MESToESB
{
    public class ESBInterface
    {
        public void send_workHour_Recive(DataTable dt)
        {
            dt.Columns["productname"].ColumnName = "MATNR";
            dt.Columns["description"].ColumnName = "MAKTX";
            dt.Columns["specno"].ColumnName = "VORNR";
            dt.Columns["sonspecno"].ColumnName = "UVORN";
            dt.Columns["specname"].ColumnName = "LTXA1";
            dt.Columns["MakeCycle"].ColumnName = "VGW06";
            if (dt == null || dt.Rows.Count <= 0)
            {
                LogHelp.WriteLog("没有可处理的实作工时数据");
            }
            foreach (DataRow row in dt.Rows)
            {
                string strXML = CreateXML(row);
                LogHelp.WriteLog("实作工时传参：" + strXML);

                

                ContainerToERP.PS_SAPServicesHttpService sap = new ContainerToERP.PS_SAPServicesHttpService();
                ContainerToERP.ErpAndAppSyncData mm = new ContainerToERP.ErpAndAppSyncData();
                mm.ReqMsg = strXML;
                ContainerToERP.ErpAndAppSyncDataResponse result = sap.ErpAndAppSyncData(mm);


                string resXML = result.RespMsg; 
                LogHelp.WriteLog("实作工时接口返回：" + resXML);
                XmlDocument myxml = new XmlDocument();
                myxml.LoadXml(resXML);
                string xml = myxml.SelectSingleNode("RespMsg/document").InnerText;
                myxml.LoadXml(xml);
                string success = myxml.SelectSingleNode("RETTYP/RTYPE").InnerText;//
                string message = myxml.SelectSingleNode("RETTYP/RTMSG").InnerText;//
                if (!string.IsNullOrWhiteSpace(success) && success == "S")
                {
                    //更新操作
                }
                else
                {
                    LogHelp.WriteLog(message);
                }
            }

        }

        public string CreateXML(DataRow row)
        {
            var strList = @"MATNR,MAKTX,VORNR,UVORN,LTXA1,VGW06,WERKS";
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", ""));
                XmlNode ReqNode = doc.CreateElement("ReqMsg");
                XmlNode headerNode = doc.CreateElement("header");
                XmlNode senderNode = doc.CreateElement("sendSysName");
                senderNode.InnerText = "APP_MES";
                XmlNode rcvNode = doc.CreateElement("rcvSysName");
                rcvNode.InnerText = "APP_ERP";
                XmlNode extConfigNode = doc.CreateElement("extConfig");
                XmlNode isReTryNode = doc.CreateElement("isReTry");
                isReTryNode.InnerText = "false";
                XmlNode FunNameNode = doc.CreateElement("FunName");
                FunNameNode.InnerText = "ZRFC_MES_WORKHOUR_RECIVE";

                extConfigNode.AppendChild(isReTryNode);
                extConfigNode.AppendChild(FunNameNode);
                headerNode.AppendChild(senderNode);
                headerNode.AppendChild(rcvNode);
                headerNode.AppendChild(extConfigNode);
                ReqNode.AppendChild(headerNode);
                XmlNode documentNode = doc.CreateElement("document");
                string strData = "";
                foreach (var _key in strList.Split(','))
                {
                    strData = string.Format("{0}<{1}>{2}</{1}>", strData, _key, row[_key].ToString());
                    
                }
                strData += "<WERKS>LYDL</WERKS>";
                documentNode.AppendChild(doc.CreateCDataSection("<root>" + strData + "</root>"));
                ReqNode.AppendChild(documentNode);
                doc.AppendChild(ReqNode);
                return doc.InnerXml;
            }
            catch (Exception e)
            {
               // LogHelp.WriteError("质量问题提起XML", e);
                return string.Empty;
            }
        }
    }
}
